﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Tetriphone
{
    public partial class PlayingField : UserControl
    {       
        

         const int FIELDWIDTH = 12;
        const int FIELDHEIGHT = 22;

        Rectangle[,] Field = new Rectangle[FIELDWIDTH, FIELDHEIGHT];
       public PlayingField()
        {
            InitializeComponent();
            for (int i = 0; i < FIELDHEIGHT; i++)
            {
                LayoutRoot.RowDefinitions.Add(new RowDefinition());
            }
            for (int i = 0; i < FIELDWIDTH; i++)
            {
                LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition());
            }
            for (int y = 0; y < FIELDHEIGHT; y++)
            {
                for (int x = 0; x < FIELDWIDTH; x++)
                {
                    Rectangle r = new Rectangle();
                    r.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    r.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                    r.StrokeThickness = 1;
                    r.Stroke = new SolidColorBrush(Colors.Black);
                    r.SetValue(Grid.RowProperty, y);
                    r.SetValue(Grid.ColumnProperty, x);
                    r.Fill = new SolidColorBrush(Colors.Red);
                    Field[x, y] = r;
                    LayoutRoot.Children.Add(r);
                }
            }
        }

        public void UpdateField(int[,] data)
        {
            for (int y = 0; y < FIELDHEIGHT; y++)
            {
                for (int x = 0; x < FIELDWIDTH; x++)
                {
                    switch (data[x, y])
                    {
                        case 0:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Black);
                            break;
                        case 1:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Blue);
                            break;
                        case 2:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Yellow);
                            break;
                        case 3:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Green);
                            break;
                        case 4:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Magenta);
                            break;
                        case 5:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Red);
                            break;
                        case 6:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Orange);
                            break;
                        case 7:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Orange);
                            break;
                        case 8:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Orange);
                            break;
                        case 9:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Orange);
                            break;
                        default:
                            Field[x, y].Fill = new SolidColorBrush(Colors.Black);
                            break;

                    }
                }
            }
            UpdateLayout();
        }
    }
}
